<template>
  <div>
    <div style="margin-bottom: 20px">
      <el-button round @click="openDialog()">创建角色</el-button>
    </div>
    <el-table
      :data="securityList"
      style="width: 100%"
      class="role_table"
      ref="securityTable"
      highlight-current-row
      @current-change="handleCurrentChange"
      border
      height="313"
    >
      <el-table-column type="index" label="序号" width="100" align="center">
      </el-table-column>
      <el-table-column
        label="入职日期"
        width="200"
        align="center"
        property="create_time"
      ></el-table-column>
      <el-table-column
        label="姓名"
        width="200"
        align="center"
        property="name"
      ></el-table-column>
      <el-table-column
        label="任职"
        width="200"
        align="center"
        property="menus"
      ></el-table-column>
      <el-table-column
        label="授权时间"
        width="200"
        align="center"
        property="auth_time"
      ></el-table-column>
      <el-table-column
        label="授权人"
        width="200"
        align="center"
        property="auth_name"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="openDialog(scope.row)">编辑</el-button>
          &nbsp;&nbsp;&nbsp;
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentPage"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 新增窗口 -->
    <el-dialog title="创建角色" :visible.sync="dialogFormVisible" width="400px">
      <el-form
        :model="security"
        status-icon
        ref="securityForm"
        label-width="100px"
        label-position="left"
        style="width: 90%"
        :rules="securityRules"
      >
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="security.name"
            placehilder="请输入角色名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="任职" prop="menus">
          <el-checkbox-group v-model="security.menus">
            <el-checkbox
              v-for="(role, index) in security.roleList"
              :key="index"
              :label="role"
              name="menus"
              style="display: block"
            >
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="!_id ? addData('securityForm') : handleUpdate('securityForm')"
          >确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import securityApi from "@/api/security";
import roleApi from "@/api/roles";
import memoryUtils from "@/utils/memoryUtils";
export default {
  data() {
    return {
      _id: null,
      auth_name: memoryUtils.user.username,
      total: 0,
      size: 5, //当前页个数
      currentPage: 1, //当前页
      dialogFormVisible: false,
      auth_time: Number,
      securityChange: null,
      security: {
        name: "",
        menus: [],
        roleList: [],
      },
      securityRules: {
        name: [
          { required: true, message: "角色名称必须输入", trigger: "blur" },
        ],
        menus: [
          { required: true, message: "请至少选择一个权限", trigger: "change" },
        ],
      },
      currentRow: null,
      securityList: [], //存储安保列表
    };
  },

  components: {},
  mounted() {
    this.condition();
    this.getrole();
  },
  methods: {
    // 获取分页信息
    condition() {
      securityApi.securityList(this.currentPage, this.size).then((response) => {
        const res = response.data;
        if (res.status == 0) {
          this.total = res.data.total;
          this.securityList = res.data.data;
          if (this.securityList == 0) {
            this.currentPage = this.currentPage - 1;
            this.condition();
          }
        } else {
          this.$message({
            message: res.msg,
            type: "warning",
          });
        }
      });
    },
    // 改变当前页位置
    handleCurrentPage(currentPage) {
      this.currentPage = currentPage;
      // console.log(currentPage);
      this.condition();
    },
    // 每页条数
    handleSizeChange(size) {
      // console.log(size);
      this.size = size;
      this.condition();
    },
    // 自带函数获取当前点击行，获取两个参数，一个当前点击的一个为上一次点击的行
    handleCurrentChange(val) {
      // console.log(val);
      this.securityChange = val;
    },
    // 删除
    handleDelete(index, row) {
      console.log(index, row);
      securityApi.securityDelete(row).then((response) => {
        const res = response.data;
        // console.log(res);
        if (res.status == 0) {
          this.$message({
            message: res.data,
            type: "success",
          });
          this.condition();
        } else {
          this.$message({
            message: res.msg,
            type: "warning",
          });
        }
      });
    },
    //打开弹窗
    openDialog(row) {
      // console.log(row._id);
      let rows = row || {}
      this.dialogFormVisible = true;
      if (rows._id) {
        this._id = row._id;
        this.security.name = row.name;
        this.security.menus = row.menus;
      } else {
        // 异步事件
        this.$nextTick(() => {
          this.$refs["securityForm"].resetFields();
        });
      }
    },
    // 创建角色
    addData(securityForm) {
      this.$refs["securityForm"].validate((valid) => {
        if (valid) {
          securityApi
            .securityAdd(
              this.security.name,
              this.security.menus,
              this.auth_name
            )
            .then((result) => {
              const res = result.data;
              if (res.status == 0) {
                this.$message({
                  message: res.data,
                  type: "success",
                });
              } else {
                this.$message({
                  message: res.msg,
                  type: "warning",
                });
              }
            });
          this.condition();
        }
        this.dialogFormVisible = false;
      });
    },
    // 更新角色
    handleUpdate(securityForm) {
      this.securityChange.name = this.security.name;
      this.securityChange.menus = this.security.menus;
      this.securityChange.auth_name = this.auth_name;
      this.$refs["securityForm"].validate((valid) => {
        if (valid) {
          // console.log(this.securityChange)
          securityApi.updateSecurity(this.securityChange).then((security) => {
            const res = security.data;
            console.log(res);
            if (res.status == 0) {
              this.$message({
                message: "更新成功",
                type: "success",
              });
              this.condition();
            } else {
              this.$message({
                message: res.msg,
                type: "warning",
              });
            }
          });
        }
        this.dialogFormVisible = false;
      });
    },
    // 获取角色权限列表
    getrole() {
      roleApi.getRolesList().then((role) => {
        // console.log(role);
        const roleList = role.data.data;
        roleList.forEach((item) => {
          // console.log(item.name);
          this.security.roleList.push(item.name);
        });
      });
    },
  },
};
</script>

<style scoped></style>